package com.zm.cc.web;

import com.zm.cc.Result;
import com.zm.cc.Utils;
import com.zm.cc.entity.Fav;
import com.zm.cc.entity.User;
import com.zm.cc.service.BizException;
import com.zm.cc.service.IFavService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/pageinfo")
public class PageInfoServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String addr = req.getParameter("addr");
        System.out.println(addr);
        Document document = Jsoup.parse(new URL(addr),2000);
        Element title = document.selectFirst("head>title");
        Element description = document.selectFirst("meta[name$='escription']");
        Element keywords = document.selectFirst("meta[name$='eywords']");
        if(keywords==null){
            keywords = document.selectFirst("meta[name$='eyword']");
        }
        System.out.println("title = " + title);
        System.out.println("description = " + description);
        System.out.println("keywords = " + keywords);
        Map<String,Object> map = new HashMap<>();
        map.put("title",title.text());
        map.put("description",description.attr("content"));
        map.put("keywords",keywords.attr("content"));
        Utils.print(resp,map);
    }
}
